import { getUser } from "@/api/user";
import { logoutAPI } from "@/api/login";
import * as TYPE from "../action-type";
import { removeToken } from "@/utils/auth/index";
import store from "..";

const UserAction = {
  // 获取用户信息
  getUserInfo() {
    return new Promise(async (resolve, reject) => {
      try {
        let res = await getUser();
        resolve({
          type: TYPE.USER_GETUSERINFO,
          info: res,
        });
      } catch (error) {
        reject({
          type: "ERROR",
        });
      }
    });
  },

  setUserInfo(user) {
    return {
      type: TYPE.USER_SETUSERINFO,
      info: user,
    };
  },
  //退出登录，主动行为，调用接口
  async logout() {
    await logoutAPI(); //退出登录接口
    //清除token
    removeToken();
    //清除store中的数据
    return {
      type: TYPE.USER_LOGOUT,
    };
  },
  //被强制退出，被动行为，不需要调用接口
  forceOffline() {},
};

export default UserAction;
